﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace QuickTweet.Common.Controls
{
    public class AutoCapitalizeTextBox : TextBox
    {
        protected override void OnTextChanged(TextChangedEventArgs e)
        {            
            base.OnTextChanged(e);

            int selectionStart = SelectionStart;
            bool firstCharacter = true;
            var characters = new char[Text.Length];
            for (int i = 0; i < Text.Length; i++)
            {
                var c = Text[i];
                if (char.IsLetter(c) && firstCharacter)
                {
                    characters[i] = char.ToUpper(c);
                    firstCharacter = false;
                }
                else
                {
                    characters[i] = c;
                }
            }

            Text = new string(characters);
            SelectionStart = selectionStart;
        }
    }
}
